﻿using System.Reflection;
using System.Windows;
using System.Windows.Threading;
using AdventureWorks.Services;
using Xomega.Framework.Lookup;
using Xomega.Framework;
using System.ServiceModel;
using System;
using System.Xml;
using System.Xml.Linq;
using System.Windows.Data;
using System.Windows.Controls;
using System.Diagnostics;

namespace AdventureWorks.Client.WPF
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            Assembly asm = typeof(EmployeeKey).Assembly;
            LookupCache.Get(LookupCache.Global).LoadFromXml(asm.GetManifestResourceStream(asm.GetName().Name + ".enumerations.xml"), false);
        }

        private void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            e.Handled = true;
            HandleException(e.Exception);
        }

        public static void HandleException(Exception ex)
        {
            ErrorList err = null;
            FaultException<ErrorList> fex = ex as FaultException<ErrorList>;
            if (fex != null) err = fex.Detail;
            if (err == null)
            {
                err = new ErrorList();
                err.Add(new ErrorMessage("EXCEPTION", ex.ToString()));
            }

            Errors.Show(err);
        }
    }
}
